import scrapy

from scrapy_movie_099.items import ScrapyMovie099Item


class MvSpider(scrapy.Spider):
    name = "mv"
    allowed_domains = ["www.dygod.net"]
    start_urls = ["https://www.dygod.net/html/gndy/china/index.html"]
    base_url = 'https://www.dygod.net'

    def parse(self, response):
        a_list = response.xpath('//table//b/a[@title]')
        for a in a_list:
            url = self.base_url + a.xpath('./@href').extract_first()
            name = a.xpath('./@title').extract_first()
            yield scrapy.Request(url=url, callback=self.parse_second, meta={'name': name})
        pass

    def parse_second(self, response):
        src = self.base_url + response.xpath('//div[@id="Zoom"]//img[1]/@src').extract_first()
        name = response.meta['name']
        movie = ScrapyMovie099Item(src=src, name=name)
        print(movie)
        yield movie
